package util;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/*
 * @author mkyong
 *
 */
public class ImageTest {

    private static int IMG_WIDTH = 100;
    private static int IMG_HEIGHT = 100;

    public static void resizeImage(String src, String des) {

        try {
            BufferedImage originalImage = ImageIO.read(new File(src));
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", new File(des));

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

    }
    
    //BACKUP CODE
    
//    public static void resizeImage(String fileName) {
//
//        try {
//            BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mkyong.jpg"));
//            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
//
//            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
//            ImageIO.write(resizeImageJpg, "jpg", new File("c:\\image\\mkyong_jpg.jpg"));
//
//            BufferedImage resizeImagePng = resizeImage(originalImage, type);
//            ImageIO.write(resizeImagePng, "png", new File("c:\\image\\mkyong_png.jpg"));
//
//            BufferedImage resizeImageHintJpg = resizeImageWithHint(originalImage, type);
//            ImageIO.write(resizeImageHintJpg, "jpg", new File("c:\\image\\mkyong_hint_jpg.jpg"));
//
//            BufferedImage resizeImageHintPng = resizeImageWithHint(originalImage, type);
//            ImageIO.write(resizeImageHintPng, "png", new File("c:\\image\\mkyong_hint_png.jpg"));
//
//        } catch (IOException e) {
//            System.out.println(e.getMessage());
//        }
//
//    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
        BufferedImage resizedImage = new BufferedImage(getIMG_WIDTH(), getIMG_HEIGHT(), type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, getIMG_WIDTH(), getIMG_HEIGHT(), null);
        g.dispose();

        return resizedImage;
    }

    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) {

        BufferedImage resizedImage = new BufferedImage(getIMG_WIDTH(), getIMG_HEIGHT(), type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, getIMG_WIDTH(), getIMG_HEIGHT(), null);
        g.dispose();
        g.setComposite(AlphaComposite.Src);

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        return resizedImage;
    }

    /**
     * @return the IMG_WIDTH
     */
    public static int getIMG_WIDTH() {
        return IMG_WIDTH;
    }

    /**
     * @param aIMG_WIDTH the IMG_WIDTH to set
     */
    public static void setIMG_WIDTH(int aIMG_WIDTH) {
        IMG_WIDTH = aIMG_WIDTH;
    }

    /**
     * @return the IMG_HEIGHT
     */
    public static int getIMG_HEIGHT() {
        return IMG_HEIGHT;
    }

    /**
     * @param aIMG_HEIGHT the IMG_HEIGHT to set
     */
    public static void setIMG_HEIGHT(int aIMG_HEIGHT) {
        IMG_HEIGHT = aIMG_HEIGHT;
    }
}